import re
import sys
from nltk.stem import *
from GetStopWords import GetStopWrods
class Cut():

    def __init__(self, listOfStopWords = []):
        self.regList = [r".*: .*\n", r">", r"[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+", r"\$\d"]
        self.replaceItems = [r"|", r"<", r">", r"(", r")", r"[", r"]", r",", r".", r"?", r"!", r"\"", r"-", r"_", r":", r"~", r"*", r"&"]
        self.listOfStopWords = listOfStopWords


    def replaceWithStopWordsAndRegex(self, filename):
        f = open(filename)
        try:
            fContext = f.read()
        except UnicodeDecodeError:
            f.close()
            return ""
        f.close()
        for item in self.regList:
            pattern = re.compile(item)
            fContext = pattern.sub("", fContext)
        for item in self.replaceItems:
            fContext = fContext.replace(item, " ")
        return fContext

    def cutWords(self, filename):
        fContext = self.replaceWithStopWordsAndRegex(filename)
        if fContext == "":
            return []
        fContext = fContext.lower()
        listOfF = fContext.split()
        listOfF = list(set(listOfF))
        for item in self.listOfStopWords:
            if item in listOfF:
                listOfF.remove(item)
        #self.wordStemming(listOfF)
        return listOfF
    
    def wordStemming(self, listOfF):
        porterStemmer = lancaster.LancasterStemmer()
        for i in range(len(listOfF)):
            listOfF[i] = porterStemmer.stem(listOfF[i])

    def Call(self, filename):
        return self.cutWords(filename)


# stopWordsPath = "stopwords.txt"
# g = GetStopWrods()
# stopWords = g.Call(stopWordsPath)
# c = Cut(stopWords)
# print(c.Call("52558"))